//
//  ZZAnimationButton.m
//  ZZInputView_Example
//
//  Created by 郑程旭 on 2022/2/25.
//  Copyright © 2022 qq282424020. All rights reserved.
//

#import "ZZAnimationButton.h"

@interface ZZAnimationButton ()
@property (nonatomic, strong) CABasicAnimation *animation;
@end

@implementation ZZAnimationButton

- (void)setBgImage:(NSString *)bgImage {
    _bgImage = bgImage;
    
    _bgImgv.image = [UIImage imageNamed:bgImage];

}

- (void)setNormalImage:(NSString *)normalImage {
    _normalImage = normalImage;
    
    [_iconBtn setImage:[UIImage imageNamed:_normalImage] forState:UIControlStateNormal];
}

- (void)setSelectedImage:(NSString *)selectedImage {
    _selectedImage = selectedImage;
    
    [_iconBtn setImage:[UIImage imageNamed:_selectedImage] forState:UIControlStateSelected];
}


- (instancetype)init {
    self = [super init];
    if (self) {
        self.layer.cornerRadius = 25;
        self.layer.masksToBounds = YES;
        
        [self addTarget:self action:@selector(clickAction) forControlEvents:UIControlEventTouchUpInside];
        
        _bgImgv = [[UIImageView alloc] init];
        _bgImgv.userInteractionEnabled = NO;
        [self addSubview:_bgImgv];
        [_bgImgv mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_equalTo(0);
        }];
        
        UIView *grView = [[UIView alloc] init];
        grView.userInteractionEnabled = NO;
        grView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.3];
        [self addSubview:grView];
        [grView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_equalTo(0);
        }];
     
        _iconBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _iconBtn.userInteractionEnabled = NO;
        [self addSubview:_iconBtn];
        [_iconBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_equalTo(0);
        }];

    }
    return self;
}

- (void)clickAction {
    
    CAAnimation *animation = [_bgImgv.layer animationForKey:@"rotationAnimation"];
    if (animation) {
        if (_bgImgv.layer.speed == 0) {
            [self resumeAnimation];
            _iconBtn.selected = YES;
        } else {
            [self pauseAnimation];
            _iconBtn.selected = NO;
        }
        
    } else {
        [self rotationAnimation];
        _iconBtn.selected = YES;
    }

}

#pragma mark - 动画效果
- (void)pauseAnimation {
    CFTimeInterval pauseTime = [_bgImgv.layer convertTime:CACurrentMediaTime() fromLayer:nil];
    _bgImgv.layer.timeOffset = pauseTime;
    _bgImgv.layer.speed = 0;
}

- (void)resumeAnimation {
    CFTimeInterval pauseTime = _bgImgv.layer.timeOffset;
    CFTimeInterval begin = CACurrentMediaTime() - pauseTime;
    [_bgImgv.layer setTimeOffset:0];
    [_bgImgv.layer setBeginTime:begin];
    _bgImgv.layer.speed = 1;
}

- (void)rotationAnimation {
    CABasicAnimation *basic = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    basic.removedOnCompletion = NO;
    [basic setToValue:@(2*M_PI)];
    [basic setDuration:6];
    [basic setRepeatCount:HUGE_VALF];
    [_bgImgv.layer addAnimation:basic forKey:@"rotationAnimation"];
}

- (void)scaleAnimation {
    CABasicAnimation *basic = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    basic.removedOnCompletion = NO;
    [basic setFromValue:@1];
    [basic setToValue:@2];
    [basic setDuration:6];
    basic.removedOnCompletion = NO;
    basic.fillMode = kCAFillModeForwards;
    [_bgImgv.layer addAnimation:basic forKey:nil];
}



@end
